Page({
  data:{
      week:['日', '一', '二', '三', '四', '五', '六'],
      block:[]
  },
  onLoad:function(options){
    this.getToday();
  },
  getFirstDayWeek:function(year,month){
     return new Date(year,month,1).getDay();
  },
  getTotalDay:function(year,month){
     return new Date(year,month+1,0).getDate();
  },
  getPrevMonthDate:function(year,month){
     return new Date(year,month,0).getDate();
  },
  getLastDay:function(year,month){
     return new Date(year,month+1,0).getDay();
  },
  onHandelTap:function(event){
      var mark = event.currentTarget.dataset.handel;
      const year = this.data.year;
      const month = this.data.month;
      if(mark=="next"){
          var newMonth = month + 1;
          var newYear = year;
          if(newMonth>11){
              newYear = year + 1;
              newMonth = 0;
          }
          this.getHandelGrid(newYear,newMonth);
      }else{
          var newMonth = month - 1;
          var newYear = year;
          if(newMonth<0){
              newYear = year - 1;
              newMonth = 11;
          }
          this.getHandelGrid(newYear,newMonth);
      }
      this.setData({
          year:newYear,
          month:newMonth,
      })
  },
  getHandelGrid:function(year,month){
      var block = [];
      var day = [];
      var nextMonth = [];

      //上月最后一天几号
      var prevLastDate = this.getPrevMonthDate(year,month);
      for(var i=0;i<this.getFirstDayWeek(year,month);i++){
          block.unshift(prevLastDate);
          prevLastDate--;
      }

      for(var i=1;i<=this.getTotalDay(year,month);i++){
          day.push(i);
      }

      //本月最后一天星期几
      var lastDay = this.getLastDay(year,month);
      //下个月第一天几号
      var nextMonthDay = new Date(year,month+1,1).getDate();
      //不等于6 只需要补全位置
      for(var i=lastDay;i<6;i++){
          nextMonth.push(nextMonthDay);
          nextMonthDay++;
      }
      this.setData({day:day,block:block,nextMonth:nextMonth});
  },
  getToday:function(){
      var date = new Date();
      var year = date.getFullYear();
      var month = date.getMonth();
      this.getHandelGrid(year,month);
      this.setData({
          year:year,
          month:month
      })
  },
  onBackTap:function(){
      this.getToday();
  }
})